<template>
  <div>
    <el-form :model="addData" ref="addform" label-width="120px" size="small">
      <el-form-item label="活动名称">
        <el-input v-model="addData.name" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item  label="开始日期">
        <el-date-picker v-model="addData.start_time" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" style="width: 80%"></el-date-picker>
      </el-form-item>
      <el-form-item  label="结束日期">
        <el-date-picker v-model="addData.end_time" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" style="width: 80%"></el-date-picker>
      </el-form-item>
      <el-form-item label="每日签到积分">
        <el-input v-model="addData.integral" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item label="活动背景">
        <select-img @selectData="data => addData.pic_url_activity = data[0]"/>
      </el-form-item>
      <el-form-item v-if="addData.pic_url_activity !== ''">
        <l-pic :picurl="addData.pic_url_activity" :size="{width:100, height:100}" :disdel="false" :disview="false"/>
      </el-form-item>
      <el-form-item label="签到背景">
        <select-img @selectData="data => addData.pic_url_sign = data[0]"/>
      </el-form-item>
      <el-form-item v-if="addData.pic_url_sign !== ''">
        <l-pic :picurl="addData.pic_url_sign" :size="{width:100, height:100}" :disdel="false" :disview="false"/>
      </el-form-item>

      <el-form-item label="补签">
        <el-switch v-model="addData.supplementary" active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
      </el-form-item>
      <div v-if="addData.supplementary === '1'">
        <el-form-item label="单次补签费用(元)">
          <el-input v-model="addData.supplementary_price" placeholder="请输入" style="width: 80%"/>
        </el-form-item>
        <el-form-item label="最多补签次数">
          <el-input v-model="addData.supplementary_number" placeholder="请输入" style="width: 80%"/>
        </el-form-item>
      </div>

      <el-form-item label="连续签到">
        <el-switch v-model="addData.continuous" active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
      </el-form-item>

      <div v-if="addData.continuous === '1'">
        <el-form-item :label="index === 0 ? '连续签到天数' : ''" 
          v-for="(item, index) in addData.continuous_arr" :key="index">
          <el-input v-model="item.days" placeholder="请输入" style="width: 20%"/>
          <span style="display:inline-block;padding:0 5px 0 20px">获取</span>
          <el-select v-model="item.give_type" style="width: 20%">
            <el-option label="积分" value="1"/>
            <el-option label="优惠券" value="2"/>
            <el-option label="实物商品" value="3"/>
          </el-select>
          <el-input v-if="item.give_type === '1'" v-model="item.give_value" placeholder="请输入" style="width: 20%"/>
          <el-select v-else-if="item.give_type === '2'" v-model="item.give_value" style="width:20%">
            <el-option v-for="item in couponList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
          <el-select v-else v-model="item.give_value" style="width:20%">
            <el-option v-for="item in goodsList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
          <el-button type="text" v-if="index === 0" @click="add">添加</el-button>
          <el-button type="text" v-else @click="addData.continuous_arr.splice(index,1)">删除</el-button>
        </el-form-item>
      </div>
      
      <el-form-item label="签到说明">
        <el-input v-model="addData.remark" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item :label="index === 0 ? '打卡语录' : ''"
       v-for="(item, index) in quotations" :key="index">
        <el-input v-model="quotations[index]" placeholder="请输入" style="width: 80%"/>
        <el-button type="text" v-if="index === 0" @click="quotations.push('')">添加</el-button>
        <el-button type="text" v-else @click="quotations.splice(index,1)">删除</el-button>
      </el-form-item>
      
      <el-form-item label="状态">
        <el-switch v-model="addData.status" active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
      </el-form-item>
    </el-form>

    <div style="text-align:center">
      <el-button type="primary" size="small" style="padding:8px 30px;" @click="submit">提 交</el-button>
    </div>
  </div>
</template>

<script>
import { getCouponList, postSignin, putSignin } from '@/api/marketing'
import { getGoodsList } from '@/api/goods'
import SelectImg from '@/components/SelectImg'
import LPic from '@/components/lPic'

export default {
  components: {
    SelectImg,
    LPic
  },
  props: {
    addData: {
      type: Object,
      required: true
    },
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      couponList: [],
      goodsList: [],
      quotations: this.addData.quotations.split(',')
    }
  },
  watch: {
    quotations(val) {
      if(val) {
        this.addData.quotations = ""
        val.forEach(item => {
          if(item.trim() !== '') {
            this.addData.quotations += item + ','
          }
        });
      }
    }
  },
  mounted() {
    this.getCouponList();
    this.getGoodsList();
  },
  methods: {
    /**
     * 提交
     */
    submit() {
      this.$refs.addform.validate((valid) => {
        if(valid) {
          if(this.isAdd){
            postSignin(this.addData).then(response => {
              if(response.status === 200){
                this.$emit('success');
                this.$message.success('添加成功！');
              }else{
                this.$message.error(response.message);
              }
            })
          }else{
            putSignin(this.addData).then(response => {
              if(response.status === 200){
                this.$emit('success');
                this.$message.success('修改成功！');
              }else{
                this.$message.error(response.message);
              }
            })
          }
        }
      })    
    },
    /**
     * 获取优惠券列表
     */
    getCouponList() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        type: '9',
      };
      getCouponList(params).then(res => {
        if(res.status === 200) {
          this.couponList = res.data;
        }else if(res.status === 204) {
          this.couponList = [];
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 获取商品列表
     */
    getGoodsList() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
      };
      getGoodsList(params).then(res => {
        if(res.status === 200) {
          this.goodsList = res.data;
        }else if(res.status === 204) {
          this.goodsList = [];
        }else{
          this.$message.error(res.message);
        }
      })
    },
    add() {
      if(this.addData.continuous_arr.length < 3) {
        this.addData.continuous_arr.push({days: '', give_type: '1', give_value: ''});
      }else{
        this.$message.warning('最多只能添加三组')
      }
    }
  },
}
</script>